/**
 * 
 */
package com.efast.cafe.portal.service.company.impl;

import com.efast.cafe.commponent.query.service.impl.QueryServiceImpl;
import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.constant.SecurityConstant;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.pager.Pagination;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.framework.service.CafeServiceImpl;
import com.efast.cafe.framework.util.QueryStringUtil;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.bean.company.*;
import com.efast.cafe.portal.bean.query.AbstractFieldType;
import com.efast.cafe.portal.bean.query.QueryBean;
import com.efast.cafe.portal.bean.query.QueryConditionBean;
import com.efast.cafe.portal.bean.query.QueryOpBean;
import com.efast.cafe.portal.dao.company.PortalCompanyOrgDao;
import com.efast.cafe.portal.dao.company.PortalCompanyUserDao;
import com.efast.cafe.portal.dao.company.PortalCompanyUserReporttoDao;
import com.efast.cafe.portal.entity.company.*;
import com.efast.cafe.portal.remote.company.PortalCompanyUserService;
import com.efast.cafe.portal.service.company.IPortalCompanyUserQueryService;
import com.efast.cafe.portal.service.company.IPortalCompanyUserService;
import com.efast.cafe.util.SetUtil;
import com.efast.cafe.util.StringUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * 企业用户管理-Service实现层
 * 
 * @author xl
 * 
 */
@Service("apiindex475-439")
@Transactional
@RestController
public class PortalCompanyUserQueryServiceImpl extends QueryServiceImpl implements IPortalCompanyUserQueryService {



	@Autowired
	private PortalCompanyUserDao portalCompanyUserDao;

	/**
	 * 处理参数信息 返回模板查询的结果集
	 * @param querys
	 * @return
	 */
	@Override
	public QueryPageBase queryPageList(QueryBean querys){
		QueryPageBase queryPageBase = new QueryPageBase();
		QueryConditionBean bean = querys.getQueryConditionBean();
//		AbstractFieldType.parameters.clear();
//		AbstractFieldType.index = 0;
		
		if (bean != null) {
			if (bean.getStart() == null || bean.getLength() == null) {
				bean.setStart(0);
				bean.setLength(querys.getTemplate().getPagenation());
			}
			querys.setStart(bean.getStart());
			querys.setLength(bean.getLength());
			queryPageBase.setStart(bean.getStart());
			queryPageBase.setLength(bean.getLength());
			if (querys.getLength() == null || querys.getStart() == null) {
				querys.setLength(bean.getLength());
				querys.setStart(bean.getStart());
			}

		}

		Map<String, Object> map = new HashMap<>();
		map.put("querys", querys);
		map.put("parameter", querys.getParameter());
		queryPageBase.setStart(querys.getStart());
		queryPageBase.setLength(querys.getLength());
		PortalCompanyUserQueryBean queryBean = new PortalCompanyUserQueryBean() ;
		queryBean.setStart(bean.getStart());
		queryBean.setLength(bean.getLength());

		if(querys.getQueryConditionBean().getQueryOpBeans()!=null && querys.getQueryConditionBean().getQueryOpBeans().size()>0){
			for (QueryOpBean queryOpBean:
					querys.getQueryConditionBean().getQueryOpBeans()) {
				this.setPropertyDescriptor(queryBean,queryOpBean.getKey(),queryOpBean.getValue()) ;
			}
			queryPageBase.setResultList(portalCompanyUserDao.queryUserInfoByOrgList(queryBean));
			queryPageBase.setTotalRows(portalCompanyUserDao.queryUserInfoByOrgCount(queryBean));
		}
		return queryPageBase;
	}


	public void setPropertyDescriptor(Object obj, String propertyName,String value) {//根据需求，定制 自己的get和set方法
		Class<?> clazz = obj.getClass();//
		Method setMethod = null;
		Method getMethod = null;
		try {
			Field field = clazz.getDeclaredField(propertyName);// 根据字段名来获取字段
			if (field != null) {
				// 构建方法的后缀
				String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
				setMethod = clazz.getDeclaredMethod("set" + methodEnd, new Class[] { field.getType() });
				String fieldType = field.getType().getSimpleName();
				if ("String".equals(fieldType)) {
					setMethod.invoke(obj, value);
				} else if ("Integer".equals(fieldType)
						|| "int".equals(fieldType)) {
					Integer intval = Integer.parseInt(value);
					setMethod.invoke(obj, intval);
				} else if ("Long".equalsIgnoreCase(fieldType)) {
					Long temp = Long.parseLong(value);
					setMethod.invoke(obj, temp);
				} else if ("Boolean".equalsIgnoreCase(fieldType)) {
					Boolean temp = Boolean.parseBoolean(value);
					setMethod.invoke(obj, temp);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

}
